﻿using DotNet.Data;
using DotNet.Helper;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BusClear
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                BusClearCore();
            }
        }

        private static void BusClearCore()
        {
            Console.WriteLine("清理僵尸车服务已启动...");
            while (true)
            {
                var sql = @"
                            delete from daolizhan
                             where busselfid in
                                   (select busselfid
                                      from (select busselfid, (sysdate - sj) * 24 * 60 * 60 c
                                              from daolizhan)
                                     where c >= 600)";
                try
                {
                    using (var db = new Database())
                    {
                        var rows = db.Execute(sql);
                        var msg = $"清除僵尸车辆:{rows}";
                        Console.WriteLine($"{DateTimeHelper.FormatDateHasSecond(DateTime.Now)} -> {msg}");
                        LogHelper.Log(msg);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("清除僵尸车辆:" + e.Message);
                }
                Thread.Sleep(60000);
            }
        }
    }
}
